package com.smartthinking.dive;


import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.app.Activity;
import android.view.View.OnTouchListener;
import android.content.Intent;
import android.content.SharedPreferences;

public class SplashScreenActivity extends Activity implements OnTouchListener{

	private static long time = 1000;
	private Sleep task;
	private boolean imhere;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_splash_screen);
		init();
	}

	private void init() {
		View splash = (View) findViewById(R.id.splash);
		imhere = true;
		splash.setOnTouchListener(this);
		task = new Sleep();
		task.execute("");
	}

	void doNext() {
		SharedPreferences preferences = getSharedPreferences("prefs", MODE_PRIVATE);
		if(preferences.getString("isregistered", "no").equals("si")){
			startActivity(new Intent(getApplicationContext(), Principal.class));
			overridePendingTransition(R.anim.fadein, R.anim.fadeout);
			finish();
		}else{
			startActivity(new Intent(getApplicationContext(), Registro.class));
			overridePendingTransition(R.anim.fadein, R.anim.fadeout);
			finish();
			
		}
		
		
		
	}

	class Sleep extends AsyncTask<String, Void, Void> {
		@Override
		protected Void doInBackground(String... params) {
			try {
				Thread.sleep(time);
			} catch (Exception e) {
			}
			publishProgress((Void) null);
			return null;
		}

		@Override
		protected void onProgressUpdate(Void... values) {
			if(imhere)doNext();
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		return false;
	}

	public boolean onTouch(View v, MotionEvent event) {
		imhere = false;
		doNext();
		return false;
	}
}